package MyGO.controller;

import MyGO.Form.TrackForm;
import MyGO.dao.ITrackFormDao;
import MyGO.dao.impl.TrackFormImpl;
import MyGO.property.CheckPro;
import MyGO.service.ICheackProService;
import MyGO.service.impl.CheckProServiceImpl;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.AnchorPane;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;


public class Shou_fei {
    @FXML
    private AnchorPane now;

    @FXML
    private TextField binglihao;

    @FXML
    private TableView<CheckPro> dingdan;

    @FXML
    private TextField inxingming;

    @FXML
    private Button jiaofei;

    @FXML
    private Label money;

    @FXML
    private TextField nianling;

    @FXML
    private TextField outxingming;

    @FXML
    private TextField shenfenzhenghao;

    @FXML
    private Button sure;

    @FXML
    private TextField xingbie;


    private TrackForm temp;
    private ICheackProService service = new CheckProServiceImpl();
    private Map<CheckPro, SimpleBooleanProperty> selectionMap = new HashMap<>();
    private List<TrackForm> temp1; //找到的人
    double all = 0.0;
    public void initialize() {
        dingdan.getColumns().clear();
        //搜索
        sure.setOnAction(e -> {
            if ((inxingming.getText() != null) || (binglihao.getText() != null)) {
                ITrackFormDao need = new TrackFormImpl();
                if (inxingming.getText() != null)  //名字搜索
                    temp1 = need.selectTrackFormByName(inxingming.getText());
                else                               //ID搜索
                    temp1 = need.selectTrackFormByName(binglihao.getText());
            }
            //找不到
            if (temp1.isEmpty()){
                Alert alert = new Alert(Alert.AlertType.ERROR);
                alert.setHeaderText("找不到该人");
                inxingming.clear();
                binglihao.clear();
                alert.showAndWait();
            }
            else{
                //显示患者信息
                temp = temp1.get(0);
                outxingming.setText(temp.getName());
                nianling.setText(String.valueOf(temp.getPatientInfoForm().getYear()));
                xingbie.setText(temp.getPatientInfoForm().getGender());
                shenfenzhenghao.setText(temp.getPatientInfoForm().getIdNum());
                outxingming.setEditable(false);
                nianling.setEditable(false);
                xingbie.setEditable(false);
                shenfenzhenghao.setEditable(false);
                //录入检查
                List<CheckPro> checkPros = new ArrayList<>();
                HashMap<String, CheckPro> checkProHashMap = service.getAllCheckPro();
                for (var ChackProID : temp.getCheckForm().getCheckProIDs()) {
                    checkPros.add(checkProHashMap.get(ChackProID));
                    selectionMap.put(checkProHashMap.get(ChackProID), new SimpleBooleanProperty(false));
                }
                //药物转化为检查
                CheckPro medicineToCheckPro = new CheckPro("MEDICINE", "药方", "无", temp.getMedicineForm().getFee(), null);
                checkPros.add(medicineToCheckPro);
                selectionMap.put(medicineToCheckPro, new SimpleBooleanProperty(false));
                //实现检查
                ObservableList<CheckPro> data = FXCollections.observableArrayList(checkPros);
                TableColumn<CheckPro, String> nameColumn = new TableColumn<>("项目名称");
                TableColumn<CheckPro, String> specificColumn = new TableColumn<>("规格");
                TableColumn<CheckPro, String> priceColumn = new TableColumn<>("价格");
                TableColumn<CheckPro, String> idColumn = new TableColumn<>("编号");
                TableColumn<CheckPro, Boolean> chooseColumn = new TableColumn<>("选中");
                //设置列宽
                nameColumn.setPrefWidth(250);
                specificColumn.setPrefWidth(250);
                priceColumn.setPrefWidth(250);
                idColumn.setPrefWidth(250);

                nameColumn.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getName()));
                specificColumn.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getSpecific()));
                priceColumn.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getPriceStr()));
                idColumn.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getId()));
                chooseColumn.setCellValueFactory(cellData -> selectionMap.get(cellData.getValue()));
                chooseColumn.setCellFactory(col -> new TableCell<CheckPro, Boolean>() {
                    private final CheckBox checkBox = new CheckBox();

                    @Override
                    protected void updateItem(Boolean item, boolean empty) {
                        super.updateItem(item, empty);

                        if (empty) {
                            setGraphic(null);
                        } else {
                            setGraphic(checkBox);
                            checkBox.setSelected(item);

                            // 监听CheckBox的选中事件
                            checkBox.setOnAction(e -> {
                                CheckPro checkPro = getTableView().getItems().get(getIndex());
                                selectionMap.get(checkPro).setValue(!selectionMap.get(checkPro).getValue());
                                all = 0;
                                selectionMap.forEach((key, value) -> {
                                    System.out.println("bn");
                                    if (value.getValue()){
                                        System.out.println("nb");
                                        all += key.getPrice();
                                    }
                                });
                                money.setText(String.valueOf(all));
                            });
                        }
                    }
                });
                dingdan.setItems(data);
                dingdan.setEditable(true);
                dingdan.getColumns().addAll(nameColumn, priceColumn, specificColumn, idColumn, chooseColumn);

                //缴费按钮
                jiaofei.setOnAction(ev ->{
                    selectionMap.forEach((person, selectedProperty) -> {
                        if (selectedProperty.get()) {
                            if (person.getId().equals("MEDICINE"))
                                temp.setStatus_vail("未发药");
                            else
                                temp.setStatus_main("未检查");
                        }
                    });
                    //结束
                    Alert alert = new Alert(Alert.AlertType.INFORMATION);
                    alert.setContentText("缴费完成，请进行下一步操作");
                    alert.showAndWait();
                    now.getChildren().clear();
                });
            }
        });
    }
}